﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace MemoryDuosControl
{
    public class CookieAwareWebClient : WebClient
    {
        private String cookie;

        public CookieAwareWebClient()
            : this("")
        {
        }

        public CookieAwareWebClient(String c)
        {
            if (!c.StartsWith("sessionID=")) c = "sessionID=" + c;
            this.cookie = c;
            this.Encoding = Encoding.UTF8;
        }

        public CookieContainer CookieContainer { get; set; }

        protected override WebRequest GetWebRequest(Uri address)
        {
            //Grabs the base request being made
            var request = (HttpWebRequest)base.GetWebRequest(address);
            //Adds the existing cookie container to the Request
            request.Headers.Add(HttpRequestHeader.Cookie, cookie);

            return request;
        }
    }
}
